﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WebService.DataAccess;using WebService.BusinessEntities;
using WebService.DataAccess.Generic;
using System.Globalization;

namespace WebService.DataAccess
{
    /// <summary>
    /// A factory object that is responsible for taking a GameResult
    /// and generating the corresponding SQL to insert that
    /// GameResult into the database. It also includes a method
    /// to grab the returned ID from the call and updating the
    /// GameResult object with it.
    /// </summary>
    internal class GameResultInsertFactory : IDbToBusinessEntityNameMapper, IInsertFactory<GameResult>
    {
        /// <summary>
        /// Creates the GameResultInsertFactory to build an insert statement for
        /// the given GameResult object.
        /// </summary>
        /// <param name="GameResult">New GameResult to insert into the database.</param>
        public GameResultInsertFactory()
        {
        }

        #region IInsertFactory<GameResult> Members

        public DbCommand ConstructInsertCommand(Database db, GameResult gameResult)
        {
            DbCommand command = db.GetStoredProcCommand("dbo.WRITE_GAME_RESULT");

            db.AddInParameter(command, "completion_date", DbType.DateTime, gameResult.completion_date);
            db.AddInParameter(command, "ids", DbType.String, gameResult.ids);
            db.AddInParameter(command, "money", DbType.String, gameResult.money);
            db.AddInParameter(command, "type", DbType.String, gameResult.type);
            db.AddInParameter(command, "start_date", DbType.DateTime, gameResult.start_date);
            db.AddInParameter(command, "room_name", DbType.String, gameResult.room_name);
            
            return command;
        }

        public void SetNewID(Database db, DbCommand command, GameResult gameResult)
        {
            //TODO: Provide set mapping
        }

        #endregion

        #region IDbToBusinessEntityNameMapper Members
        /// <summary>
        /// Maps a field name in the database (usually a parameter name for a stored proc)
        /// to the corresponding business entity property name.
        /// </summary>
        /// <remarks>This method is intended for error message handling, not for reflection.</remarks>
        /// <param name="dbParameter">Name of field/parameter that the database knows about.</param>
        /// <returns>Corresponding business entity field name.</returns>
        public string MapDbParameterToBusinessEntityProperty(string dbParameter)
        {
            switch (dbParameter)
            {
                case "completion_date":
                    return "completion_date";
                case "ids":
                    return "ids";
                case "money":
                    return "money";
                case "type":
                    return "type";
                case "start_date":
                    return "start_date";
                case "room_name":
                    return "room_name";
                default:
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter);
            }
        }
        #endregion
    }

}

